package com.fsc.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.AnnRepoNews;
import com.fsc.entity.domain.StudentActivity;
import com.fsc.entity.dto.ActivityUpdateDTO;
import com.fsc.entity.dto.NewsDTO;
import com.fsc.entity.dto.NewsUpdateDTO;
import com.fsc.entity.dto.StudentActivityDTO;
import com.fsc.entity.vo.AnnRepoNewsVO;
import com.fsc.entity.vo.LoginUserVO;
import com.fsc.entity.vo.StudentActivityVO;
import com.fsc.service.StudentActivityService;
import com.fsc.util.OssUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static com.fsc._enum.ReturnEnum.SUCCESS;

@RestController
@RequestMapping("/activity/")
public class StudentActivityController {
    @Autowired
    private StudentActivityService studentActivityService;
    @Resource
    private RedisTemplate<String, byte[]> redisByteTemplate;
    @PostMapping("{id}")
    public ResultWrapper<List<StudentActivityVO>> getNews(@PathVariable("id") int id){
        return studentActivityService.getNews(id);
    }

    /**
     * 编辑的时候获取所有
     * @param schoolId
     * @param authorId
     * @return
     */
    @PostMapping("get")
    public ResultWrapper<List<StudentActivity>> getNews(
            @RequestParam("schoolId") int schoolId,
            @RequestParam("authorId") int authorId) {
        return studentActivityService.getActivity(schoolId,authorId);
    }

    @PostMapping("add")
    public ResultWrapper<String> add(@RequestBody StudentActivityDTO studentActivityDTO) throws IOException {
        String iconUrl = studentActivityDTO.getPicture();
        if(Boolean.FALSE.equals(redisByteTemplate.hasKey(iconUrl))){
            return ResultWrapper.fail(ReturnEnum.UPLOAD_FAIL);
        }
        OssUtil.uploadImage(redisByteTemplate.opsForValue().get(iconUrl),iconUrl);
        redisByteTemplate.delete(iconUrl);
        StudentActivity studentActivity = StudentActivity.builder()
                .authorId(studentActivityDTO.getAuthorId())
                .schoolId(studentActivityDTO.getSchoolId())
                .title(studentActivityDTO.getTitle())
                .content(studentActivityDTO.getContent())
                .picture(studentActivityDTO.getPicture())
                .time(LocalDateTime.now())
                .build();
        studentActivityService.save(studentActivity);
        return ResultWrapper.success(SUCCESS);
    }

    @PostMapping("update")
    public ResultWrapper<String> updateNews(@RequestBody ActivityUpdateDTO activityUpdateDTO){
        // 保存更新后的记录到数据库
        studentActivityService.update(Wrappers.<StudentActivity>lambdaUpdate().eq(StudentActivity::getId,activityUpdateDTO.getId())
                .set(StudentActivity::getAuthorId,activityUpdateDTO.getAuthorId())
                .set(StudentActivity::getSchoolId,activityUpdateDTO.getSchoolId())
                .set(StudentActivity::getTime, LocalDateTime.now())
                .set(StudentActivity::getTitle,activityUpdateDTO.getTitle())
                .set(StudentActivity::getPicture,activityUpdateDTO.getPicture())
                .set(StudentActivity::getContent,activityUpdateDTO.getContent()));

        return ResultWrapper.success(SUCCESS);
    }

    @DeleteMapping("del/{id}")
    public ResultWrapper<String> deleteNews(@PathVariable("id") long id){
        studentActivityService.removeById(id);
        return ResultWrapper.success(SUCCESS);
    }
}
